﻿using d1Common.WebController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity;
using d1Project.Modules.Common.do4A.Models;
using System.Threading.Tasks;
using d1Common;
using d1Project.Modules.Common.do4A.Domain;
using d1Project.Tools;
using System.ComponentModel.DataAnnotations;

namespace d1Project.Modules.Common.do4A.Controllers.a2
{
    [Route("do4A/a2/User")]
    public class User22Controller : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private do4ADbContext db = new do4ADbContext();

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Post(User2PostVm model)
        {
            if (!ModelState.IsValid) this.Error("验证不通过");

            if (AuthenticationMgr.Instance.Token2_useVerficationCode)
            {
                VerificationCodeMgr.Instance.checkVerificationCode(this.db, model.VerificationId, model.VerificationCode, AuthenticationMgr.Instance.Token2_VerficationMaxErrorCount);
            }

            if (!string.IsNullOrEmpty(model.MobilePhone))
            {
                var old = await db.User2s.Where(x => x.MobilePhone == model.MobilePhone).FirstOrDefaultAsync();
                if (old != null) this.Error("电话不能重复");
            }
            if (!string.IsNullOrEmpty(model.Email))
            {
                var old = await db.User2s.Where(x => x.Email == model.Email).FirstOrDefaultAsync();
                if (old != null) this.Error("邮箱不能重复");
            }

            User2 entity = new User2();
            entity.Id = doServices.Instance.DoCore.getGUID();
            entity.Name = model.Name;
            entity.Icon = model.Icon;
            entity.MobilePhone = model.MobilePhone;
            entity.Email = model.Email;
            entity.IsAvailable = "1";
            entity.CreateTime = DateTime.Now;
            entity.UpdateTime = DateTime.Now;
            db.User2s.Add(entity);

            User2Pwd user2pwd = new User2Pwd();
            user2pwd.Id = entity.Id;
            if (string.IsNullOrEmpty(model.Password))
            {
                user2pwd.Password = TextHelper.GetMd5("123456");
            }
            else
            {
                user2pwd.Password = model.Password;
            }

            db.User2Pwds.Add(user2pwd);

            await db.SaveChangesAsync();
            return Ok();
        }

    }


    public class User2PostVm
    {
        /// <summary>
        /// 编码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 手机
        /// </summary>
        [Required]
        public string MobilePhone { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 验证码Id
        /// </summary>
        public string VerificationId { get; set; }

        /// <summary>
        /// 验证码值
        /// </summary>
        public string VerificationCode { get; set; }
    }
}
